#include "object.h"

int const Object::writeToFile(FILE *f)
{
    double buf;
    /*buf = POS.getX();
    if (!fwrite(&buf, sizeof(double), 1, f))
        return 0;
    buf = POS.getY();
    if (!fwrite(&buf, sizeof(double), 1, f))
        return 0;
    buf = POS.getZ();
    if (!fwrite(&buf, sizeof(double), 1, f))
        return 0;
    buf = DIR.getX();
    if (!fwrite(&buf, sizeof(double), 1, f))
        return 0;
    buf = DIR.getY();
    if (!fwrite(&buf, sizeof(double), 1, f))
        return 0;
    buf = DIR.getZ();
    if (!fwrite(&buf, sizeof(double), 1, f))
        return 0;
    buf = UP.getX();
    if (!fwrite(&buf, sizeof(double), 1, f))
        return 0;
    buf = UP.getY();
    if (!fwrite(&buf, sizeof(double), 1, f))
        return 0;
    buf = UP.getZ();
    if (!fwrite(&buf, sizeof(double), 1, f))
        return 0;*/
    if (fwrite(&(buf = POS.getY()), sizeof(double), 1, f) &&
        fwrite(&(buf = POS.getY()), sizeof(double), 1, f) &&
        fwrite(&(buf = POS.getZ()), sizeof(double), 1, f) &&
        fwrite(&(buf = DIR.getX()), sizeof(double), 1, f) &&
        fwrite(&(buf = DIR.getY()), sizeof(double), 1, f) &&
        fwrite(&(buf = DIR.getZ()), sizeof(double), 1, f) &&
        fwrite(&(buf = UP.getX()), sizeof(double), 1, f) &&
        fwrite(&(buf = UP.getY()), sizeof(double), 1, f) &&
        fwrite(&(buf = UP.getZ()), sizeof(double), 1, f))
        return 1;
    else
        return 0;
}

int const Object::writeToFile(char *name)
{
    double buf;
    FILE *f;
    f = fopen(name, "wb");
    if (!f)
        return 0;
    fwrite(&(buf = POS.getX()), sizeof(double), 1, f);
    fwrite(&(buf = POS.getY()), sizeof(double), 1, f);
    fwrite(&(buf = POS.getZ()), sizeof(double), 1, f);
    fwrite(&(buf = DIR.getX()), sizeof(double), 1, f);
    fwrite(&(buf = DIR.getY()), sizeof(double), 1, f);
    fwrite(&(buf = DIR.getZ()), sizeof(double), 1, f);
    fwrite(&(buf = UP.getX()), sizeof(double), 1, f);
    fwrite(&(buf = UP.getY()), sizeof(double), 1, f);
    fwrite(&(buf = UP.getZ()), sizeof(double), 1, f);
    fclose(f);
    return 1;
}

int Object::readFromFile(FILE *f)
{
    double buf;
    if (!fread(&buf, sizeof(double), 1, f))
        return 0;
    POS.setX(buf);
    if (!fread(&buf, sizeof(double), 1, f))
        return 0;
    POS.setY(buf);
    if (!fread(&buf, sizeof(double), 1, f))
        return 0;
    POS.setZ(buf);
    if (!fread(&buf, sizeof(double), 1, f))
        return 0;
    DIR.setX(buf);
    if (!fread(&buf, sizeof(double), 1, f))
        return 0;
    DIR.setY(buf);
    if (!fread(&buf, sizeof(double), 1, f))
        return 0;
    DIR.setZ(buf);
    if (!fread(&buf, sizeof(double), 1, f))
        return 0;
    UP.setX(buf);
    if (!fread(&buf, sizeof(double), 1, f))
        return 0;
    UP.setY(buf);
    if (!fread(&buf, sizeof(double), 1, f))
        return 0;
    UP.setZ(buf);

    DIR.normalize();
    UP.normalize();

    return 1;
}

int Object::readFromFile(char *name)
{
    FILE *f;
    f = fopen(name, "rb");
    if (!f)
        return 0;
    double buf;
    fread(&buf, sizeof(double), 1, f);
    POS.setX(buf);
    fread(&buf, sizeof(double), 1, f);
    POS.setY(buf);
    fread(&buf, sizeof(double), 1, f);
    POS.setZ(buf);
    fread(&buf, sizeof(double), 1, f);
    DIR.setX(buf);
    fread(&buf, sizeof(double), 1, f);
    DIR.setY(buf);
    fread(&buf, sizeof(double), 1, f);
    DIR.setZ(buf);
    fread(&buf, sizeof(double), 1, f);
    UP.setX(buf);
    fread(&buf, sizeof(double), 1, f);
    UP.setY(buf);
    fread(&buf, sizeof(double), 1, f);
    UP.setZ(buf);
    fclose(f);

    DIR.normalize();
    UP.normalize();

    return 1;
}

void Object::rotateUpGrad(double angle)
{
    double a = pi * angle / 180;
    Vector3 newUP((UP * cos(a)) - (DIR * sin(a)));
    DIR = (DIR * cos(a)) + (UP * sin(a));
    UP = newUP;
}

void Object::rotateUpRad(double angle)
{
    Vector3 newUP((UP * cos(angle)) - (DIR * sin(angle)));
    DIR = (DIR * cos(angle)) + (UP * sin(angle));
    UP = newUP;
}

void Object::rotateDownGrad(double angle)
{
    double a = pi * angle / 180;
    Vector3 newUP(-(UP * cos(a)) - (DIR * sin(a)));
    DIR = (DIR * cos(a)) - (UP * sin(a));
    UP = newUP;
}

void Object::rotateDownRad(double angle)
{
    Vector3 newUP(-(UP * cos(angle)) - (DIR * sin(angle)));
    DIR = (DIR * cos(angle)) - (UP * sin(angle));
    UP = newUP;
}

void Object::rotateLeftGrad(double angle)
{
    double a = pi * angle / 180;
    DIR = (DIR * cos(a)) + (UP * DIR * sin(a));
}

void Object::rotateLeftRad(double angle)
{
    DIR = (DIR * cos(angle)) + (UP * DIR * sin(angle));
}

void Object::rotateRightGrad(double angle)
{
    double a = pi * angle / 180;
    DIR = (DIR * cos(a)) - (UP * DIR * sin(a));
}

void Object::rotateRightRad(double angle)
{
    DIR = (DIR * cos(angle)) - (UP * DIR * sin(angle));
}
